#pragma once
#include <cstddef>
#include <cstdint>
#include <memory>
#include <mutex>
#include <string>
#include <thread>
#include <unordered_map>
#include <vector>
#include "LoggerFile.h"

namespace xncc {
struct ParamStaticInfo;
class AsyncLogQueue;
class LoggerEngine
{
  public:
    LoggerEngine();
    ~LoggerEngine();
    LoggerEngine(LoggerEngine&&)      = delete;
    LoggerEngine(const LoggerEngine&) = delete;

    auto operator=(LoggerEngine&&) -> LoggerEngine& = delete;
    auto operator=(LoggerEngine&) -> LoggerEngine&  = delete;

    void start();
    auto createAsyncQueue() -> AsyncLogQueue*;
    void addLogFile(std::shared_ptr<LoggerFile>, uint16_t fileId, int coreId);
    auto storeParamsInfo(const ParamStaticInfo*) -> int32_t;

  private:
    void handleLogMsg(const char* msg, uint32_t len);
    void decodeLogMsgVesion(const char* msg, size_t len, int32_t fmtId);
    void decodeLogParams(const char* msg, size_t len);

    void ConsumeQueue(AsyncLogQueue* queue);
    void flushFile();
    void run();

    std::string                                               currentLogStr_;
    std::thread                                               logThread_;
    std::vector<AsyncLogQueue*>                               tmpQueues_;
    std::vector<AsyncLogQueue*>                               deletedQueues_;
    std::mutex                                                queues_mutex;
    std::vector<AsyncLogQueue*>                               queues;
    std::unordered_map<uint16_t, std::shared_ptr<LoggerFile>> userLogfiles;
    std::vector<const ParamStaticInfo*>                       paramStaticInfoVec;
    std::mutex                                                fmtIdMutex;
    int                                                       coreId_;
};

}  // namespace xncc